//
//  StatusCell.swift
//  weibo10
//
//  Created by 晁岳山 on 15/10/19.
//  Copyright © 2015年 晁岳山. All rights reserved.
//

import UIKit
import SnapKit
class StatusCell: UITableViewCell {
    
    var viewModel: StatusViewModel? {
        didSet {
            topView.viewModel = viewModel
            contentLabel.text = viewModel?.status.text
            pictureView.viewModel = viewModel
            //修改配图视图的大小
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
                //顶部约束  如果没有图片top上就没有距离
                let offSet = viewModel?.thumbnailURLs?.count == 0 ? 0 : StatusCellPictureItemMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(offSet)
            }
        }
    }
    
    func rowHeight(vm: StatusViewModel) -> CGFloat {
        //设置内容  cell的高度 跟配图的宽高有关 级viewModel有关
        viewModel = vm
        //设置完成以后 强制更新约束
        layoutIfNeeded()
        //根据底部视图 返回cell的高度
        return CGRectGetMaxY(bottomView.frame)
    }
    //懒加载需要的控件
    //顶部视图
    private var topView: StatusCellTopView = StatusCellTopView()
    //正文
    private var contentLabel: UILabel = UILabel(content: "微博正文",
        fontSize: 13,
        color: UIColor.darkGrayColor(),
        scrrenInsert: CGFloat (StatusCellMargin))
    //配图
    private var pictureView: StatusCellPictureView = StatusCellPictureView()
    //底部视图
    private var  bottomView: StatusCellBottomView = StatusCellBottomView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - cell的布局
extension StatusCell {
    
    func setupUI() {
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        //自动布局
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(StatusCellMargin * 2  + StatusCellWidth)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            make.right.equalTo(contentView.snp_right).offset(-StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            make.width.equalTo(200)
            make.height.equalTo(100)
        }
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            //向下得约束  在自动计算行高的过程中，这个在最下面一个控件必须有
//            make.bottom.equalTo(contentView.snp_bottom)

        }
    }
}